package com.qianyu.versioncontrolapi.config;


import com.qianyu.versioncontrolcore.entity.base.Result;
import com.qianyu.versioncontrolcore.exception.CustomerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * @author wangzhic
 * @data 2020/8/4 13:45
 * 功能描述 全局异常处理类
 **/
@RestControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

//    @ExceptionHandler(value = Exception.class)
//    public Result<Object> handle(HttpServletResponse response, Exception exception) {
//        response.setCharacterEncoding("utf-8");
//        return Result.error(exception.getMessage());
//    }

    /**
     * 拦截自定义异常
     * @param response
     * @param exception
     * @return
     */
    @ExceptionHandler(value = CustomerException.class)
    public Result<Object> handleCustomerException(HttpServletResponse response, CustomerException exception) {
        response.setCharacterEncoding("utf-8");
        return Result.error(exception.getCode(),exception.getMessage());

    }


}
